package com.cspg.design.presenter;

import android.content.Intent;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.cspg.design.CSPGApplication;
import com.cspg.design.CheckPermissionsActivity;
import com.cspg.design.MainActivity;
import com.cspg.design.bean.UserInfo;
import com.cspg.design.model.UserModel;
import com.cspg.design.request.Login;
import com.cspg.design.ui.login.LoginActivity;
import com.cspg.design.widget.CSPGProgressBar;
import com.cspg.design.widget.ToastUtils;

import java.util.Date;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;

public class LoginPresenter implements IPresenter {

    private Login loginModel;
    private AppCompatActivity activity;

    private CSPGProgressBar cspgProgressBar;

    public LoginPresenter(CheckPermissionsActivity activity) {
        this.loginModel = new Login(this);
        this.activity = activity;
    }

    public LoginPresenter(AppCompatActivity activity) {
        this.loginModel = new Login(this);
        this.activity = activity;
    }


    public void login(final  String username, final  String pwd)  {
//        activity.showProgress();
        if(cspgProgressBar == null)
        {
            cspgProgressBar = CSPGProgressBar.show(activity);
        }else {
            cspgProgressBar.show();
        }
        try {

            loginModel.loginNoCode( username,  pwd);
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void getLoginFailure(String msg) {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, msg);
    }

    @Override
    public void netWorkError(String netError) {
//        activity.hideProgress();
//        activity.toast(netError);

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, netError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestFailure(String noDataError) {
//        activity.hideProgress();
////        activity.toast(noDataError);
        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        ToastUtils.toast(activity, noDataError);
        if (activity instanceof MainActivity) {
            //activity.openNewActivity(LoginActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, LoginActivity.class);
            activity.startActivity(intent);
        }
    }

    @Override
    public void httpRequestSuccess() {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (activity instanceof LoginActivity) {
            LoginActivity loginActivity = (LoginActivity) activity;
            loginActivity.openNewActivity(MainActivity.class);
        } else {
//            activity.openNewActivity(MainActivity.class);
            Intent intent = new Intent();
            intent.setClass(activity, MainActivity.class);
            activity.startActivity(intent);
        }

    }

    public void loginSuccess(UserModel userModel)
    {

        if (null != cspgProgressBar && cspgProgressBar.isShowing()) {
            cspgProgressBar.dismiss();
        }
        if (activity instanceof LoginActivity) {
            LoginActivity loginActivity = (LoginActivity) activity;
            loginActivity.openNewActivity(MainActivity.class);
            //进行获取待办工单
            UserInfo userInfo = UserInfo.getInstance();
            userInfo.CompanyName = userModel.getEmployeeData().getCompanyName();
            userInfo.EmployeeID = userModel.getEmployeeData().getEmployeeID();
            userInfo.EmployeeName = userModel.getEmployeeData().getEmployeeName();
            userInfo.UserRight = userModel.getEmployeeData().getUserRight();
        }

    }

}